package com.threeox.commonlibrary.util;

import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.threeox.commonlibrary.config.CommonConfig;
import com.threeox.commonlibrary.inter.IOverallConfig;
import com.threeox.commonlibrary.ui.view.inter.engine.model.IBaseViewControl;
import com.threeox.utillibrary.util.EmptyUtils;
import com.threeox.utillibrary.util.LogUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 *
 * @ClassName: ViewUtils
 *
 * @Description: Todo(View的工具类)
 *
 * @author 赵屈犇
 *
 * @date 创建时间:2016/12/20 14:59
 *
 * @version 1.0
 */
public class ViewUtils {

    private static final String TAG = ViewUtils.class.getName();
    private CommonConfig mCommonConfig = null;
    private IOverallConfig mOverallConfig = null;

    private ViewUtils() {
        this.mCommonConfig = CommonConfig.getInstance();
        this.mOverallConfig = mCommonConfig.getOverallExtend();
    }

    public static ViewUtils newInstance() {
        return new ViewUtils();
    }

    /**
     * 注入View
     *
     * @param data
     * @param tag
     * @param v
     */
    public void injectView(Object data, String tag, View v) {
        Object result = ValueUtils.getObjValue(data, tag);
        setValue(v, result);
    }

    /**
     * 注入View
     *
     * @param data
     * @param tag
     * @param v
     */
    public void injectView(JSONObject data, String tag, View v) {
        Object result = ValueUtils.getObjValue(data, tag);
        setValue(v, result);
    }

    /**
     * 注入根据对象中的Key
     *
     * @param obj
     * @param view
     */
    public void inJectViewByTag(Object obj, View view) {
        try {
            if (obj instanceof JSONObject) {
                Set<String> set = ((JSONObject) obj).keySet();
                for (String key : set) {
                    setValue(view.findViewWithTag(key), ((JSONObject) obj).get(key));
                }
            } else {
                Field[] fields = obj.getClass().getDeclaredFields();
                for (Field field : fields) {
                    field.setAccessible(true);
                    Object val = field.get(obj);
                    setValue(view.findViewWithTag(field.getName()), val);
                }
            }
        } catch (Exception e) {
            LogUtils.e(TAG, "inJectViewByTag失败啦:" + e.getMessage());
        }
    }

    /**
     * 得到View的值
     *
     * @param supView
     * @param tag
     * @return
     */
    public Object getViewVal(ViewGroup supView, String tag) {
        View view = supView.findViewWithTag(tag);
        if (EmptyUtils.isEmpty(view)) {
            return null;
        }
        Object result = null;
        if (view instanceof IBaseViewControl) {
            result = ((IBaseViewControl) view).getValue();
        } else if (view instanceof TextView) {
            result = ((TextView) view).getText().toString();
        } else if (view instanceof WebView) {
            result = ((WebView) view).getUrl();
        }
        if (mOverallConfig != null) {
            Object value = mOverallConfig.getViewVal(view);
            if (null != value) {
                result = value;
            }
        }
        return result;
    }

    /**
     * 设置值
     *
     * @param view
     * @param val
     */
    public void setValue(View view, Object val) {
        try {
            if (view != null && val != null) {
                if (view instanceof ImageView) {
                    if (val instanceof Drawable) {
                        ((ImageView) view).setImageDrawable((Drawable) val);
                    }
                    //setImage(view, val.toString());
                } else if (view instanceof TextView) {
                    ((TextView) view).setText(val.toString());
                } else if (view instanceof IBaseViewControl) {
                    ((IBaseViewControl) view).setValue(val);
                } else if (view instanceof WebView) {
                    ((WebView) view).loadUrl(val.toString());
                }
                if (mOverallConfig != null) {
                    mOverallConfig.setViewValue(view, val);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 得到所有View的Tag
     *
     * @param viewGroup
     * @return
     */
    public static List<Object> getViewTags(ViewGroup viewGroup){
        List<Object> allTag = new ArrayList<>();
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            try {
                View view = viewGroup.getChildAt(i);
                Object tag = view.getTag();
                if (tag != null)
                    allTag.add(tag);
                //&& !(view instanceof ListModelBaseView)
                if (view instanceof ViewGroup && !(view instanceof IBaseViewControl)) {
                    List<Object> tags = getViewTags((ViewGroup) view);
                    if (EmptyUtils.isNotEmpty(tags))
                        allTag.addAll(tags);
                }
            } catch (Exception e) {
                LogUtils.e(TAG, "getViewTags失败:" + e.getMessage());
            }
        }
        return allTag;
    }

}
